package person.bangbang.im.Androidgin.Util;

import java.lang.reflect.Field;

import person.bangbang.im.Androidgin.Androidgin;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
/**
 * get System build information, useful for debug.
 *
 * @author bangbang.song@gmail.com
 */
public final class SystemInfo {
	public static String toStr() {
		Class<Build> c;
		try{
			String ret = "";
			// nested class '$'
			c = (Class<Build>) Class.forName("android.os.Build$VERSION");
			Field[] fields = c.getFields();
			
			for (Field f : fields) {
				ret += "[" + f.getName() + ':' + f.get(c).toString() + ']';
			}
			c = (Class<Build>) Class.forName("android.os.Build");
			fields = c.getFields();
			
			for (Field f : fields) {
				ret += "[" + f.getName() + ':' + f.get(c).toString() + ']';
			}
			
			return ret;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} finally {

		}
		

		return "";
	}
	
	public static String getVersion(Context con, boolean getCode){
		String body = "";
       	PackageManager pm = con.getPackageManager();
    	PackageInfo info = null;
    	try {
			info = pm.getPackageInfo(con.getPackageName(), 0);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			
			int code = info.versionCode;
			String name = info.versionName;
			
			int major = code >> 24;
			int minor = (code >> 16) & 0xF;
			int mcode = (code) & 0xFF;
			
//			body += "versionCode: " + code + "\n";
			if (getCode) {
				body += "V" + major + "." + minor + "." + mcode;
			} else {
				body += name;	
			}

		}
		
		return body;
	}
}
